<template>
  <div style="margin: 20px;">
    课程列表
    <el-form :inline="true" :model="queryCourse" class="demo-form-inline">
      <el-form-item>
        <el-input v-model="queryCourse.title" placeholder="课程名称"></el-input>
      </el-form-item>
      <el-form-item>
        <el-select v-model="queryCourse.status" placeholder="课程状态" clearable>
          <el-option label="未发布" value="Draft"></el-option>
          <el-option label="已发布" value="Normal"></el-option>
        </el-select>
      </el-form-item>
      <el-form-item>
        <el-button type="primary" @click="onQuery(1)">查询</el-button>
        <el-button @click="queryCourse={}">清空</el-button>
      </el-form-item>
    </el-form>

    <el-table :data="tableData" border fit>
      <el-table-column label="序号" align="center" width="80" v-slot="scope">
        {{ scope.$index + 1 }}
      </el-table-column>
      <el-table-column prop="title" label="课程名称"/>
      <el-table-column label="课程状态" align="center" width="95" v-slot="scope">
        {{ scope.row.status === 'Draft' ? '未发布' : '已发布' }}
      </el-table-column>
      <el-table-column prop="lessonNum" label="课时数" align="center" width="80"/>
      <el-table-column prop="eduCreate" label="添加时间" align="center" width="200"/>
      <el-table-column prop="viewCount" label="浏览数量" align="center" width="80"/>
      <el-table-column label="操作" align="center" width="520" v-slot="scope">
        <el-button type="primary" @click="toUpdate(scope.row.id)">编辑课程基本信息</el-button>
        <el-button type="primary" @click="toUpdateChapter(scope.row.id)">编辑课程大纲信息</el-button>
        <el-button type="danger" @click="onDel(scope.row.id)">删除课程基本信息</el-button>
      </el-table-column>
    </el-table>

    <el-pagination
      style="margin-top: 20px"
      background
      layout="prev, pager, next"
      :page-size="pageSize"
      :current-page="pageNum"
      @current-change="onQuery"
      :total="total"/>
  </div>
</template>

<script>
import {couDelete, couPage} from '@/api/course'

export default {
  data() {
    return {
      queryCourse: {},
      pageNum: 1,
      pageSize: 4,
      total:0,
      tableData: []
    }
  },
  created() {
    this.onQuery(1)
  },
  methods: {
    onQuery(pageNum) {
      this.pageNum=pageNum
      couPage(this.queryCourse, this.pageNum, this.pageSize).then(res => {
        this.tableData = res.data.records
        this.total = res.data.total
      })
    },
    onDel(id){
      this.$confirm('此操作将永久删除该课程, 是否继续?', '提示', {
        confirmButtonText: '确定',
        cancelButtonText: '取消',
        type: 'warning'
      }).then(() => {
        couDelete(id).then(res=>{
          if (res.code===200){
            this.$message.success('删除成功!');
          }else {
            this.$message.error('删除失败!');
          }
          location.reload();
        })
      }).catch(() => {
        this.$message.info('已取消删除')
      });
    },
    toUpdate(id){
      this.$router.push({path:'/course/save',query:{courseId:id}})
    },
    toUpdateChapter(id){
      this.$router.push({path:'/course/chapter',query:{courseId:id}})
    }
  }
}
</script>

<style>

</style>
